#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <typeinfo>
#include <fstream>
#include <fstream>
#include <iomanip>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include "utils.h"



namespace ai::facial_recognition {

    //时间戳转换为日期格式
    void timestampToTime(char *timeStamp, char *dateTime, int dateTimeSize) {
        time_t tTimeStamp = atoll(timeStamp);
        struct tm *pTm = gmtime(&tTimeStamp);
        strftime(dateTime, dateTimeSize, "%Y-%m-%d %H:%M:%S", pTm);
    }

    void convert_hex_str_to_char(const std::string &str, unsigned char *char_array) {
        for (size_t i = 0; i < str.length(); i += 2) {
            std::string byte = str.substr(i, 2);
            char chr = (char) (int) strtol(byte.c_str(), NULL, 16);
            auto j = i / 2;
            char_array[j] = chr;
        }
    }

    std::string convert_unsigned_char_pointer_to_hex(unsigned char *data, int len) {
        std::stringstream ss;
        ss << std::uppercase << std::hex << std::setfill('0');
        for (int i = 0; i < len; i++) {
            ss << std::setw(2) << static_cast<unsigned>(data[i]);
        }
        return ss.str();
    }

     //图像颜色格式转换
    int ColorSpaceConversion(MInt32 width, MInt32 height, MInt32 format, MUInt8 *imgData, ASVLOFFSCREEN &offscreen) {
        offscreen.u32PixelArrayFormat = (unsigned int) format;
        offscreen.i32Width = width;
        offscreen.i32Height = height;

        switch (offscreen.u32PixelArrayFormat) {
            case ASVL_PAF_RGB24_B8G8R8:
                offscreen.pi32Pitch[0] = offscreen.i32Width * 3;
                offscreen.ppu8Plane[0] = imgData;
                break;
            case ASVL_PAF_I420:
                offscreen.pi32Pitch[0] = width;
                offscreen.pi32Pitch[1] = width >> 1;
                offscreen.pi32Pitch[2] = width >> 1;
                offscreen.ppu8Plane[0] = imgData;
                offscreen.ppu8Plane[1] = offscreen.ppu8Plane[0] + offscreen.i32Height * offscreen.i32Width;
                offscreen.ppu8Plane[2] = offscreen.ppu8Plane[0] + offscreen.i32Height * offscreen.i32Width * 5 / 4;
                break;
            case ASVL_PAF_NV12:
            case ASVL_PAF_NV21:
                offscreen.pi32Pitch[0] = offscreen.i32Width;
                offscreen.pi32Pitch[1] = offscreen.pi32Pitch[0];
                offscreen.ppu8Plane[0] = imgData;
                offscreen.ppu8Plane[1] = offscreen.ppu8Plane[0] + offscreen.pi32Pitch[0] * offscreen.i32Height;
                break;
            case ASVL_PAF_YUYV:
            case ASVL_PAF_DEPTH_U16:
                offscreen.pi32Pitch[0] = offscreen.i32Width * 2;
                offscreen.ppu8Plane[0] = imgData;
                break;
            case ASVL_PAF_GRAY:
                offscreen.pi32Pitch[0] = offscreen.i32Width;
                offscreen.ppu8Plane[0] = imgData;
                break;
            default:
                return 0;
        }
        return 1;
    }

    std::string generate_uuid() {
        // 生成UUID
        boost::uuids::uuid uuid = boost::uuids::random_generator()();
    
        // 将UUID转换为字符串并输出
        return boost::uuids::to_string(uuid);
    }
}

